package com.moyu.shopapp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.moyu.shopapp.entity.Cart;
import com.moyu.shopapp.entity.Goods;
import com.moyu.shopapp.entity.User;
import com.moyu.shopapp.entity.vo.CartGoodsVO;
import com.moyu.shopapp.service.CartService;
import com.moyu.shopapp.service.GoodsService;
import com.moyu.shopapp.service.UserService;
import com.moyu.shopapp.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    @Autowired
    private UserService userService;

    @Autowired
    private GoodsService goodsService;

    /**
     * 添加购物车
     * 传入产生
     * goodsId
     * @param cart 购物车对象数据
     * @param token 用户token
     * @return
     */
    @PostMapping("addCart")
    public String addCart(Cart cart,String token){
        //判断用户是否登录
        if (token==""||JwtUtils.isJwtExpired(token)){
            return "用户未登录";
        }
        //购物车添加货物的数量
        int quantity = cart.getQuantity();
        //判断该货物是否有库存
        if (!goodsService.checkStock(cart.getGoodsId(),quantity)){
            return "库存不足";
        }
        //通过token获取用户id
        User userInfo = userService.getUserInfo(token);
        //给购物车添加user_id
        cart.setUserId(userInfo.getUserId());
        //查找表内是否已有该商品
        Cart cartItem = cartService.findCartByUserIdAndGoodsId(cart.getUserId(), cart.getGoodsId());
        //判断
        if (cartItem !=null){
            //已存在则修改数量
            //设置数量
            cartItem.setQuantity(cartItem.getQuantity()+ quantity);
            //更新购物车货物数量
            cartService.updateQuantity(cartItem);
        }else {
            //新插入一个
            cartService.save(cart);
        }
        //更新货物库存与销量
        //goodsService.updateQuantity(cart.getGoodsId(), quantity);
        return "成功添加购物车";
    }

    /**
     * 删除购物车
     * @param cartsId 购物车id
     * @return
     */
    @PostMapping("removeCart")
    public String removeCart(Integer[] cartsId){
        for (int i = 0; i < cartsId.length; i++) {
            cartService.removeById(cartsId[i]);
        }
        return "已删除";
    }

    /**
     * 修改购物车商品数量
     * @param cart 购物车对象
     * @return
     */
    @PostMapping("changeQuantity")
    public String changeQuantity(Cart cart){
        return cartService.updateQuantity(cart);
    }

    /**
     * 查找购物车列表
     * @param token 用户token
     * @return
     */
    @GetMapping("/findAllCart")
    public List<CartGoodsVO> findAllCart(String token){
        if (token==""||JwtUtils.isJwtExpired(token)){
            return null;
        }
        //通过token获取用户id
        User userInfo = userService.getUserInfo(token);
        return cartService.listByUserId(userInfo.getUserId());
    }

    /**
     * 获取购物车条目数
     * @param token 用户token
     * @return
     */
    @GetMapping("/findCartCountById")
    public long findCartCountById(String token){
        //判断用户是否登录
        if (token==""||JwtUtils.isJwtExpired(token)){
            return 0;
        }
        User userInfo = userService.getUserInfo(token);
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId,userInfo.getUserId());
        return cartService.count(wrapper);
    }

}
